%{
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h> // for malloc and free

extern int yylex(void);
extern void yyerror(char *);

%}

%union {
    double num;
    char str[100];
}

%type <num> E
%type <str> R

%token <num> NUMBER
%token <str> STRING

%left '='
%left '>' '<'
%left '+' '-'
%left '*' '/'
%left '(' ')'



%%

S : /*empty statement*/
    | S E  '\n'    { printf("%f\n", $2); }
    | S R
    | S E R        { printf("%f\n", $2); }
    | S '\n'
    ;

E : E '+' E { $$ = $1 + $3; }
    | E '-' E { $$ = $1 - $3; }
    | E '*' E { $$ = $1 * $3; }
    | E '/' E { $$ = $1 / $3; }
    | '(' E ')' { $$ = $2; }
    | '-' E   { $$ = -1 * $2; }
    | NUMBER  { $$ = $1; }
    ;

R : STRING { printf("this is a string : %s\n", $1); }
    ;

%%

int main(void) {
    yyparse();
    return 0;
}

void yyerror(char *s) {
    fprintf(stderr, "%s\n", s);
}